const {
  queryMaintainerListByAssetsId,
  updateWorkOrder
} = require('../../api/workOrder')
const {
  WORD_ORDER_OPERATION_TYPE
} = require('../../constant/index')
const {
  clearEmptyKey
} = require('../../utils/clearEmptyKey')
const {
  to,
  openLoading,
  closeLoading,
  showToast,
  backParentLoad
} = getApp()
Page({
  data: {
    loadingType: '',
    dataList: [],
    searchAreaValues: {},
    pagination: {
      page: 1,
      limit: 10
    },
    total: 0,
    wordData: {}
  },

  onLoad() {
    this.setData({
      wordData: JSON.parse(this.options.data)
    })

    this.resetData()
    this.getSysUserList()
  },

  async getSysUserList(openL = true) {
    const optionsData = {};
    const {
      companyId,
      assetsId
    } = this.data.wordData
    if (companyId) {
      optionsData.companyId = companyId
    }

    if (assetsId) {
      optionsData.assetsId = assetsId
    }
    const {
      searchAreaValues
    } = this.data
    const params = {
      queryCondition: searchAreaValues.inputVal || '',
      ...optionsData,
      ...this.data.pagination
    }

    clearEmptyKey(params)
    openL && openLoading()
    const [err, data] = await to(this.getApiName()(params))
    closeLoading()
    if (!err) {
      const {
        dataList
      } = this.data
      this.setData({
        dataList: dataList.concat(data.dataList),
        total: data.total
      })

      if (data.total === 0) {
        this.setData({
          loadingType: 'empty'
        })
        return
      }

      if (this.data.dataList.length >= data.total) {
        this.setData({
          loadingType: 'end'
        })
      }
    }
  },

  getApiName() {
    return queryMaintainerListByAssetsId
  },

  handleScrolltolower() {
    const {
      total,
      pagination,
      dataList
    } = this.data

    if (dataList.length < total) {
      let page = pagination.page;
      page++
      this.setData({
        loadingType: 'loading',
        ['pagination.page']: page
      })
      this.getSysUserList(false)
    }
  },


  resetData() {
    this.setData({
      ['pagination.page']: 1,
      dataList: [],
      loadingType: '',
      total: 0
    })
  },

  handleSearchChange(event) {
    this.setData({
      searchAreaValues: event.detail,
    })
    this.resetData()
    this.getSysUserList()
  },

  handleChoose(event) {
    let title =  '是否转单给' + event.detail.userName

    if (+this.options.operationType === WORD_ORDER_OPERATION_TYPE.WORK_ORDER_DISTRIBUTION) {
      title =  '是否分派给' + event.detail.userName
    }
    wx.showModal({
      content: title,
      success: (res) => {
        if (res.confirm) {
          this.submitForm(event.detail);
        }
      }
    })
  },

  async submitForm(event) {
    const params = {
      operationType: this.options.operationType,
      afterMaintainerId: event.userId,
      workOrderId: this.data.wordData.workOrderId,
    }
    openLoading();
    const [err] = await to(updateWorkOrder(params));
    closeLoading()
    if (!err) {
      showToast({
        message: '提交成功',
        type: 'success',
        mask: true,
        onClose: () => {
          setTimeout(() => {
            backParentLoad(1)
          }, 1000)
        }
      })
    }
  }
})